package com.apexsoft.test.sample.ams.controller;


import com.alibaba.fastjson.JSONObject;
import com.apexsoft.aas.common.exception.ValidException;
import com.apexsoft.aas.util.ValidatorUtil;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Max;


@RestController
@Validated
public class ValidateController {


    @RequestMapping("/rule1")
    public JSONObject rule1(@RequestParam(name = "param1", required = false) @NotEmpty String param1, @RequestParam(required = false) String param2) {
        return new JSONObject() {{
            put("code", 1);
            put("note", "访问成功");
        }};

    }


    @RequestMapping("/rule2")
    public JSONObject rule2(@RequestBody @Validated Params params) {
        return new JSONObject() {{
            put("code", 1);
            put("note", "访问成功");
        }};

    }

    @RequestMapping("/rule3")
    public JSONObject rule3(@RequestBody @Validated(value = Params.BusinessBGrouop.class) Params params) {
        return new JSONObject() {{
            put("code", 1);
            put("note", "访问成功");
        }};

    }

    @RequestMapping("/rule4")
    public JSONObject rule4(@RequestBody Params params) {
        try {
            ValidatorUtil.validate(params);
        } catch (ValidException e) {
            throw e;
        }
        return new JSONObject() {{
            put("code", 1);
            put("note", "访问成功");
        }};

    }

    public static class Params {
        public static interface BusinessAGrouop {

        }

        public static interface BusinessBGrouop {

        }

        @NotEmpty
        private String param1;

        @Max(value = 10)
        private Integer param2;


        @Max(value = 300, groups = BusinessBGrouop.class)
        private Integer param3;

        public String getParam1() {
            return param1;
        }

        public void setParam1(String param1) {
            this.param1 = param1;
        }

        public Integer getParam2() {
            return param2;
        }

        public void setParam2(Integer param2) {
            this.param2 = param2;
        }

        public Integer getParam3() {
            return param3;
        }

        public void setParam3(Integer param3) {
            this.param3 = param3;
        }
    }


}
